<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Api\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends TooController {

	//系统首页
    public function index(){
        $member=M('tmember')->where(array('id'=>session('member')['id']))->find();
        $this->assign('member',$member);
        if($member['start']==2||$member['start']==4){
            $shop=M('tshop')->where(array('id'=>$member['dqed']))->find();
        }else{
            //显示最高借贷产品
            $shop=M('tshop')->order('money desc')->find();
        }

        //轮播数据
        $lb=M("torder")->order(" id desc")->limit(5)->select();
        foreach($lb as &$v){
            //$member=M('tmember')->where(array('id'=>$v['m_id']))->find();
            $v['phone']=substr($v['yhkphone'],0,3)."****".substr($member['phone'],7,4);
        }
        $this->assign('lb',$lb);
        $this->assign('shop',$shop);
        $this->display();
    }


    //认证页面
    public function certificat(){
        $m_id=session('member')['id'];
        //是否关系认证
        $gxrz=M('tmember_g')->where(array('m_id'=>$m_id))->find();
        $this->assign('gxrz',$gxrz);

        //借贷认证
        $jdrz=M('tmember_j')->where(array('m_id'=>$m_id))->find();
        $this->assign('jdrz',$jdrz);

        //身份证认证
        $sfrz=M('tmember')->where(array('id'=>$m_id))->find();

        $this->assign('sfrz',$sfrz);

        //分享认证
        $fx=M('tfx')->where(array('m_id'=>$m_id))->find();
        $this->assign('fx',$fx);

        $this->display();
    }

    //关系认证
    public function relation(){
        $m_id=session('member')['id'];
        $model=M('tmember_g');
        if($_POST){
            $arr=array();
            $arr['gzdw']=I('post.gzdw');
            $arr['gzzw']=I('post.gzzw');
            $arr['gzdz']=I('post.gzdz');
            $arr['hyzt']=I('post.hyzt');
            $arr['lxr1']=I('post.lxr1');
            $arr['lxr2']=I('post.lxr2');
            $arr['lxr3']=I('post.lxr3');
            $arr['lxr4']=I('post.lxr4');
            $arr['lxr1n']=I('post.lxr1n');
            $arr['lxr2n']=I('post.lxr2n');
            $arr['lxr3n']=I('post.lxr3n');
            $arr['lxr4n']=I('post.lxr4n');
            $arr['yhpo']=I('post.yhpo');
            $arr['yhpon']=I('post.yhpon');
            $arr['yx']=I('post.yx');
            if(I('post.id')){

                $arr['id']=I('post.id');
                //修改
                if($model->save($arr)){

                    $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));
                }

            }else{
                //添加
                $arr['time']=date("Y-m-d H:i:s",time());
                $arr['m_id']=$m_id;

                if($model->add($arr)){
                    $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));
                }

            }
            $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));exit;
            $this->ajaxReturn(array('type'=>2,'msg'=>'操作失败'));
        }
        else{
            //得到授权产品
            $shop=M('tshop')->where("start = 1")->order(" money desc")->find();
            $this->assign('shop',$shop);
            $arr=$model->where(array('m_id'=>$m_id))->find();
            $this->assign('arr',$arr);
            $this->display();
        }

    }

    //借贷认证
    public function borrow(){

        //我的借贷认证
        $model=M('tmember_j');
        if($_FILES){
            $list=$model->where(array('m_id'=>session('member')['id']))->find();
            $arr=array();
            $arr['pz']=$this->TBD_upload($_FILES['upimg']);
            if($list['id']){
                //修改
                $arr['id']=$list['id'];
                if($model->save($arr)){
                    $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));
                }else{
                    $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));exit;
                    $this->ajaxReturn(array('type'=>2,'msg'=>'操作失败'));
                }
            }else{
                //添加
                $arr['time']=date("Y-m-d H:i:s",time());
                $arr['m_id']=session('member')['id'];
                if($model->add($arr)){
                    $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));
                }else{
                    $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));exit;
                    $this->ajaxReturn(array('type'=>2,'msg'=>'操作失败'));
                }
            }
        }

        $arr=$model->where(array('m_id'=>session('member')['id']))->find();
        $this->assign('arr',$arr);
        //查询收款二维码
        $this->assign('ewm',C('TBD_SK'));

        //生成分享二维码
        $tlewm="http://qr.liantu.com/api.php?text=http://".$_SERVER['SERVER_NAME'].U('Home/fx',array('id'=>session('member')['id']));
        $this->assign('tlewm',$tlewm);
        $this->display();
    }



    //个人认证
    public function personalr(){
        $model=M('tmember');
        if($_POST){
            $arr=array();
            $arr['id']=I('post.id');
            $arr['phone']=I('post.phone');
            $arr['name']=I('post.name');
//            $arr['yhkgs']=I('post.yhkgs');
//            $arr['yhkh']=I('post.yhkh');
//            $arr['yhkphone']=I('post.yhkphone');
            if($_FILES['preID']){
                //身份证正面
                $arr['sfz_z']=$this->TBD_upload($_FILES['preID']);
            }
            if($_FILES['backID']){
                //身份证反面
                $arr['sfz_f']=$this->TBD_upload($_FILES['backID']);
            }

            if($model->save($arr)){
                $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));exit;
                $this->ajaxReturn(array('type'=>2,'msg'=>'操作失败'));
            }

        }
        $arr=$model->where(array('id'=>session('member')['id']))->find();
//        dump(session('member'));exit;
        $this->assign('arr',$arr);
        $this->display();
    }

    //我的
    public function my(){
        $model=M('tmember');
        $member=$model->where(array('id'=>session('member')['id']))->find();
        $this->assign('member',$member);

        //我的额度
        if($member['start']==2){
            //显示最高借贷产品
            $shop=M('tshop')->where(array('id'=>$member['dqed']))->find();
        }else{
            //显示最高借贷产品
            $shop=M('tshop')->order('money desc')->find();
        }
        $this->assign('shop',$shop);

        $this->display();
    }


    public function commonproblem(){
        $arr=M('tdoc')->order("id desc")->select();
        $this->assign('arr',$arr);
        $this->display('commonproblem');
    }
    public function customerservice(){
        $this->assign('wxkf',C('TBD_WX'));
        $this->assign('wxewm',C('TBD_KF'));
        $this->display('customerservice');
    }


    //借贷添加记录
    public function jdadd(){
        if($_POST){
            $model=M('torder');
            $arr=array();
            $arr['m_id']=session('member')['id'];
            $arr['jdje']=I('post.jdje');
            $arr['ts']=I('post.ts');
            $arr['lixi']=I('post.lixi');
            $arr['yhkh']=I('post.yhkh');
            $arr['yhkgs']=I('post.yhkgs');
            $arr['yhkphone']=I('post.yhkphone');
            $arr['sxf']=I('post.sxf');
            $arr['start']=1;
            $arr['ordersn']=date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            $arr['stime']=date('Y-m-d H:i:s',time());
            $id=$model->add($arr);
            if($id){
                $this->ajaxReturn(array('type'=>1,'id'=>$id));
            }else{
                $this->ajaxReturn(array('type'=>2,'msg'=>'服务器异常'));
            }
        }
    }
    //借贷付款
    public function jdfk(){
        $model=M('torder');
        if($_FILES){

            $arr=array();
            $arr['pz']=$this->TBD_upload($_FILES['upimg']);
            $arr['id']=I('post.pid');
            $arr['start']=2;
            //修改
            if($model->save($arr)){
                $this->ajaxReturn(array('type'=>1,'msg'=>'操作成功'));
            }else{

                $this->ajaxReturn(array('type'=>2,'msg'=>'操作失败'));
            }
        }

        $arr=$model->where(array('id'=>$_GET['id']))->find();
        $this->assign('arr',$arr);
        //查询收款二维码
        $this->assign('ewm',C('TBD_SK'));
        $this->display();
    }

    //借贷列表
    public function jd(){
        $model=M('torder');
        $arr=$model->where(array('m_id'=>session('member')['id']))->order(" id desc")->select();
        $this->assign('arr',$arr);
        $this->display();
    }

}
